import WxmpRsa from "wxmp-rsa";
import { encode } from "base64-arraybuffer";
import { atob, btoa } from "./polyfill";

const PUBLIC_KEY = `TUlHZk1BMEdDU3FHU0liM0RRRUJBUVVBQTRHTkFEQ0JpUUtCZ1FDV1kxVktJWEdTdFhPTXRTa2IybnFWOUE0VjZNcWxUUHY4RGc5U2R4bThUWmdPWUpKeEZBanBVT3d0MGF1NnE1SkRUcnNsWm5nZzl1bTFJaE5KbFJMRXlTYlR2TjdCemVxNlhPcFp4NXc2WFJaKzcvbzBVaTRZdmNZd0lIQjVEZ1M1WEpuTGEzdkxxV09rNE5BdFkwbHFDMjAxNzBtSGk1Rm1qZGFrNjNPVHp3SURBUUFC`;
const PRIVATE_KEY = '';

/**
 * base64转btye数组
 * @param base64
 * @returns {Uint8Array}
 */
function base64ToArrayBuffer(base64) {
  let binary_string = atob(base64);
  let len = binary_string.length;
  let bytes = new Uint8Array(len);
  for (let i = 0; i < len; i++) {
      bytes[i] = binary_string.charCodeAt(i);
  }
  return bytes;
}
// 字符串转base64
function _encode(str){
    var encode = encodeURI(str);
    var base64 = btoa(encode);
    return base64;
}
// base64转字符串
function _decode(base64){
    var decode = atob(base64);
    var str = decodeURI(decode);
    return str;
}
export const RSAENCRY = {
    /**
     * 公钥加密
     * @param val 需要加密的字符串
     * @return string 返回加密结果
     */
    encryptByPublicKey: function (val = '') {
        if(val === ''){
            return '';
        }
        let rsa = new WxmpRsa(); // 新建WxmpRsa对象
        rsa.setPublicKey(_decode(PUBLIC_KEY)); // 设置公钥
        return rsa.encrypt(val); // 对需要加密的数据进行加密
    },
    /**
     * 公钥分段加密
     * @returns {string}
     * @param val 要加密的数据 先转为byte数组在进行解码
     */
    encryptLongByPublicKey: function (val = '') {
        if(val === ''){
            return '';
        }
        let rsa = new WxmpRsa(); // 新建WxmpRsa对象
        rsa.setPublicKey(_decode(PUBLIC_KEY)) // 设置公钥
        val = encodeURIComponent(val)
        // 前端使用encodeURIComponent进行编码，后端解密后使用decodeURIComponent解码
        let decryptStr = encode(rsa.encryptLong(val));
        return decryptStr ? decryptStr : val;
    },
    /**
     * 私钥解密
     * @param val
     * @returns {string|false|null|PromiseLike<ArrayBuffer>}
     */
    decryptByPrivateKey: function (val = '') {
        if(val === ''){
            return '';
        }
        let rsa = new WxmpRsa(); // 新建WxmpRsa对象
        rsa.setPrivateKey(PRIVATE_KEY); // 设置私钥
        return rsa.decrypt(val); // 对需要解密的数据进行解密
    }
}

export const RSADECRY = {
    /**
     * 公钥解密
     * @param secretWord
     * @returns {解密|string|false|PromiseLike<ArrayBuffer>}
     */
    decryptByPublicKey: function (val = '') {
        if (val === '') {
            return '';
        }
        let rsa = new WxmpRsa(); // 新建WxmpRsa对象
        rsa.setPublicKey(_decode(PUBLIC_KEY));
        //使用公钥对私钥加密后的数据解密
        return rsa.decrypt(val);
    },
    /**
     * 公钥分段解密
     * @returns {string}
     * @param val 要解密的数据 先转为byte数组在进行解码
     */
    decryptLongByPublicKey: function (val = '') {
        if(val === ''){
            return '';
        }
        let rsa = new WxmpRsa(); // 新建WxmpRsa对象
        rsa.setPublicKey(_decode(PUBLIC_KEY)) // 设置公钥
        // 后端使用encodeURIComponent 进行编码，前端解密后使用decodeURIComponent解码
        let decryptStr = decodeURIComponent(rsa.decryptLong(base64ToArrayBuffer(val)));
        // let decryptStr = rsa.decryptLong(base64ToArrayBuffer(val));
        // console.log(rsa.decryptLong(base64ToArrayBuffer(val)))
        return decryptStr
    }
}